package com.mall.shopping.converter;

import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dto.ProductDetailDto;
import com.mall.shopping.dto.ProductDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.LinkedList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/24-19:15
 */
@Mapper(componentModel = "spring")
public interface ProductConverter {

    @Mappings({
            @Mapping(source = "id",target = "productId"),
            @Mapping(source = "title",target = "productName"),
            @Mapping(source = "price",target = "salePrice"),
            @Mapping(source = "sellPoint",target = "subTitle"),
            @Mapping(source = "imageBig",target = "picUrl")
    })
    ProductDto item2Dto(Item item);

    List<ProductDto> items2Dto(List<Item> items);


    @Mappings({
            @Mapping(source = "item.id",target = "productId"),
            @Mapping(source = "item.title",target = "productName"),
            @Mapping(source = "item.price",target = "salePrice"),
            @Mapping(source = "item.sellPoint",target = "subTitle"),
            @Mapping(source = "item.image",target = "productImageSmall"),
            @Mapping(source = "itemDesc.itemDesc",target = "detail"),
            @Mapping(source = "item.imageBig" ,target = "productImageBig")
    })
    ProductDetailDto item2DetailDto(Item item, ItemDesc itemDesc);

    /**
     * Convert string to list
     * by lc
     * @param str
     * @return
     */
    default List<String> string2List(String str){
        if(str == null || str.length() == 0){
            return null;
        }
        String[] strings = str.split(",");
        List<String> list = new LinkedList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
        return list;
    }
}
